package com.fg.cloud.manager.service.order.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fg.cloud.common.NumberUtil;
import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.finals.LogContent;
import com.fg.cloud.common.finals.LogStatus;
import com.fg.cloud.common.finals.OrderStatus;
import com.fg.cloud.common.shiro.UserUtils;
import com.fg.cloud.manager.entry.agency.vo.AgencyUserVo;
import com.fg.cloud.manager.entry.factory.FactoryProcessEntity;
import com.fg.cloud.manager.entry.factory.vo.FactoryUserVo;
import com.fg.cloud.manager.entry.order.OrderDeliveryDetailEntity;
import com.fg.cloud.manager.entry.order.OrderDeliveryInfoEntity;
import com.fg.cloud.manager.entry.order.OrderDeliveryLogEntity;
import com.fg.cloud.manager.entry.order.OrderDetailEntity;
import com.fg.cloud.manager.entry.order.OrderInfoEntity;
import com.fg.cloud.manager.entry.order.vo.OrderAllDateVo;
import com.fg.cloud.manager.entry.order.vo.OrderDeliveryAllDateVo;
import com.fg.cloud.manager.entry.order.vo.OrderDetailVo;
import com.fg.cloud.manager.entry.order.vo.ResultDeliveryVo;
import com.fg.cloud.manager.entry.product.ProductDeliveryFile;
import com.fg.cloud.manager.mapper.order.OrderDeliveryFileMapper;
import com.fg.cloud.manager.mapper.order.OrderDeliveryLogMapper;
import com.fg.cloud.manager.mapper.order.OrderDeliveryMapper;
import com.fg.cloud.manager.service.factory.FactoryProcessService;
import com.fg.cloud.manager.service.order.OrderDeliveryService;
import com.fg.cloud.manager.service.order.OrderInfoService;

@Service
public class OrderDeliveryServiceImpl implements OrderDeliveryService{

	@Autowired
	private OrderDeliveryMapper orderDeliveryMapper;
	
	@Autowired
	private OrderDeliveryFileMapper orderDeliveryFileMapper;
	
	@Autowired
	private OrderInfoService orderInfoService;
	
	@Autowired
	private OrderDeliveryLogMapper deliveryLogMapper;
	
	@Autowired
	private FactoryProcessService factoryProcessService;

	/**
	 * 查询发货单列表
	 */
	@Override
	public void orderList(PageUtils<OrderDeliveryInfoEntity> pageinfo) {
		Long cont = orderDeliveryMapper.findListCont(pageinfo);
		pageinfo.setTotal(cont);
		if(cont > 0){
			pageinfo.setData(orderDeliveryMapper.findList(pageinfo));
		}else{
			pageinfo.setData(new ArrayList<OrderDeliveryInfoEntity>());
		}
	}

	/**
	 * 根据id查询订单和详情
	 */
	@Override
	public OrderInfoEntity findByIdAndOrderOrigin(Map<String, Object> usmap) {
		
		// 根据id查询订单
		List<OrderAllDateVo> oos = orderDeliveryMapper.findAllOrderDate(usmap);
		if(oos.size()==0){
			return null;
		}else{
			OrderInfoEntity oientity = toOrderInfo(oos);
			return oientity;
		}
	}

	/**
	 * 订单详情封装
	 * */
	private OrderInfoEntity toOrderInfo(List<OrderAllDateVo> oos) {
		OrderInfoEntity oe = new OrderInfoEntity();
		oe.setId(oos.get(0).getId());
		oe.setOrderNo(oos.get(0).getOrderNo());
		oe.setAgeId(oos.get(0).getAgeId());
		oe.setAmountPay(oos.get(0).getAmountPay());
		oe.setAmountTotal(oos.get(0).getAmountTotal());
		oe.setDeliveryDate(oos.get(0).getDeliveryDate());
		oe.setErpReceive(oos.get(0).getErpReceive());
		oe.setExpressFee(oos.get(0).getExpressFee());
		oe.setFacId(oos.get(0).getFacId());
		oe.setIsSpecialPrice(oos.get(0).getIsSpecialPrice());
		oe.setOrderDate(oos.get(0).getOrderDate());
		oe.setOrderNo(oos.get(0).getOrderNo());
		oe.setOrderSource(oos.get(0).getOrderSource());
		oe.setOrderStatus(oos.get(0).getOrderStatus());
		oe.setPassStatus(oos.get(0).getPassStatus());
		oe.setPayStatus(oos.get(0).getPayStatus());
		oe.setReceiveId(oos.get(0).getReceiveId());
		oe.setReceiveInfo(oos.get(0).getReceiveInfo());
		oe.setRemark(oos.get(0).getRemark());
		oe.setSpecialPrice(oos.get(0).getSpecialPrice());
		oe.setTaxation(oos.get(0).getTaxation());
		oe.setInvoiceId(oos.get(0).getInvoiceId());
		oe.setInvoiceName(oos.get(0).getInvoiceName());
		oe.setInvoiceInfo(oos.get(0).getInvoiceInfo());
		//oe.setUpdater(oos.get(0).getUpdater());
		//oe.setUpdateTime(oos.get(0).getUpdateTime());
		//oe.setCreateTime(oos.get(0).getCreateTime());
		//oe.setCreator(oos.get(0).getCreator());
		List<OrderDetailEntity> orderDetails = new ArrayList<OrderDetailEntity>();;
		
		for (int i = 0; i < oos.size(); i++) {
			OrderDetailVo ode = new OrderDetailVo();
			ode.setAmount(oos.get(i).getAmount());
			ode.setDeliveryAmount(oos.get(i).getDeliveryAmount());
			ode.setId(oos.get(i).getDateId());
			ode.setOrderId(oos.get(i).getOrderId());
			ode.setOrderId(oos.get(i).getOrderId());
			ode.setProductId(oos.get(i).getProductId());
			ode.setPromotionPrice(oos.get(i).getPromotionPrice());
			ode.setRemark(oos.get(i).getDateRemark());
			ode.setSellPrice(oos.get(i).getSellPrice());
			ode.setStdAmount(oos.get(i).getStdAmount());
			ode.setSumMoney(oos.get(i).getSumMoney());
			ode.setUnitId(oos.get(i).getUnitId());
			ode.setSkuId(oos.get(i).getSkuId());
			ode.setUnitName(oos.get(i).getUnitName());
			ode.setProductSpec(oos.get(i).getProductSpec());
			orderDetails.add(ode);
		}
		oe.setListDetails(orderDetails);
		return oe;
	}

	/**
	 * 添加发货单和详情
	 * */
	@Override
	@Transactional
	public Integer insertOrder(OrderDeliveryInfoEntity odi) {
		ResultDeliveryVo flag = deliver(odi);
		if(flag.getResultCode() == -1 ){// 发货失败
			return -1;
		}
		// 计算本次发货总金额
		List<OrderDeliveryDetailEntity> listDetails = odi.getDetailList() == null ? new ArrayList<OrderDeliveryDetailEntity>() : odi.getDetailList();
		BigDecimal sum = new BigDecimal(0);
		for(int i = 0;i<listDetails.size();i++){
			// 发货单发货数量*单价
			BigDecimal amount =listDetails.get(i).getPrice().multiply(listDetails.get(i).getAmount()).multiply(BigDecimal.valueOf(listDetails.get(i).getUnitNum()));
			listDetails.get(i).setAmountPrice(amount);
			sum = sum.add(amount);
		}
		// 计算运费
		OrderInfoEntity order = orderInfoService.find(odi.getSourceBillId());
		BigDecimal expressFee = new BigDecimal(0);
		expressFee = odi.getAmountTotal().divide(order.getAmountTotal(),2,BigDecimal.ROUND_HALF_UP).multiply(order.getExpressFee());  
		//查询订单,有折扣再计算折扣
		/*Map<String, Object> usmap = new HashMap<String, Object>();
		usmap.put("facId", odi.getFacId());
		usmap.put("id", odi.getSourceBillId());
		// 在此判断订单来源
		if(odi.getOrigin() == ?){
			
		}
		OrderInfoEntity of = orderDeliveryMapper.findorderInfoById(usmap);
		// 计算折扣,应付款/总额
		BigDecimal zhe = of.getAmountPay().divide(of.getAmountTotal());
		sum = sum.multiply(zhe);*/
		odi.setExpressFee(expressFee.setScale(2,BigDecimal.ROUND_HALF_UP));
		odi.setAmountTotal(sum.setScale(2,BigDecimal.ROUND_HALF_UP));
		
		//编码规则
		odi.setDeliveryNo(NumberUtil.getOrderDeliveNumber());
		
		//添加发货单
		Long deliveryinfoFlag = orderDeliveryMapper.insertDelOrder(odi);
		
		for(int i= 0;i<listDetails.size();i++){
			listDetails.get(i).setDeliveryId(odi.getId());
		}
		// 新增发货单详情
		if(listDetails.size()>0){
			orderDeliveryMapper.insertDelOrderDetail(listDetails);
		}
		// 发货单附件新增
		List<String> fileList = odi.getFileList();
		if(fileList.size() > 0){
			orderDeliveryFileMapper.insertFiles(fileList,odi.getId());
		}
		//插入日志
		deliveryLogMapper.insert(getOrderDeliveryLogEntity(odi.getId(), 
				LogContent.DELIVERY_ADD, LogStatus.DELIVERY_ADD));
		if(deliveryinfoFlag > 0){
			return 1;
		}
		return 0;
	}
	
	/**
	 * 发货单日志操作
	 * */
	private OrderDeliveryLogEntity getOrderDeliveryLogEntity(Long deliveryId,
			String content, Integer type) {
		OrderDeliveryLogEntity entity = new OrderDeliveryLogEntity();
		//判别处理用户类型
		if (UserUtils.getCurrentUser() instanceof FactoryUserVo) {
			FactoryUserVo user = (FactoryUserVo) UserUtils.getCurrentUser();
			entity.setCompanyId(user.getFacId());
			entity.setCompanyName(user.getFacName());
			entity.setOperatorId(user.getId());
			entity.setOperatorName(user.getTrueName());
		} else if (UserUtils.getCurrentUser() instanceof AgencyUserVo) {
			AgencyUserVo user = (AgencyUserVo) UserUtils.getCurrentUser();
			entity.setCompanyId(user.getAgeId());
			entity.setCompanyName(user.getAgencyName());
			entity.setOperatorId(user.getId());
			entity.setOperatorName(user.getTrueName());
		}
		entity.setDeliveryId(deliveryId);
		entity.setOperatorTime(new Date());
		entity.setContent(content);
		entity.setType(type);
		return entity;
	}

	/**
	 * 发货单点发货
	 * */
	@Override
	@Transactional
	public ResultDeliveryVo deliver(OrderDeliveryInfoEntity oe) {
		/*Map<String, Object> usmap = new HashMap<>();
		usmap.put("facId", ov.getFacId());
		usmap.put("id", ov.getId());
		OrderDeliveryInfoEntity oe = findDeliveryDatailsById(usmap);*/
		Map<String, Object> usmap = new HashMap<>();
		usmap.put("facId", oe.getFacId());
		ResultDeliveryVo rv = new ResultDeliveryVo();

		/*// 把物流信息加进去,发货仓库暂留讨论
		oe.setExpressCompany(ov.getExpressCompany());//快递公司
		oe.setExpressCompanyNo(ov.getExpressCompanyNo());//快递公司编号
		oe.setExpressNo(ov.getExpressNo());//快递单号
*/		
		// 根据单据来源查单据    并对比发货数量和剩余总数量
		Integer origin = oe.getOrigin(); // 来源
		Long sourceBillId = oe.getSourceBillId(); // 单子id	
		usmap.put("id", sourceBillId);
		if(origin == 0){// 订单
			OrderInfoEntity ord = findByIdAndOrderOrigin(usmap);
			if(deliverFlag(oe,ord)){// 发货成功
				rv.setResultCode(1);
				return rv;
			}else{// 发货失败
				rv.setResultCode(-1);
				rv.setResult(oe);
				return rv;
			}
			
		}else{// 退货单
			rv.setResultCode(666);
			return rv;
		}
	}
	
	/**
	 * 订单发货
	 * 用于判断发货是否成功和检验哪条数据出错,oe发货单数据,ord订单所有数据
	 * 订货单剩余的需要支付的金额现按照销售价计算
	 * */
	public Boolean deliverFlag (OrderDeliveryInfoEntity oe,OrderInfoEntity ord){
		Boolean flag = true;
		int p = 1; // 用于判断是否发货完
		List<OrderDeliveryDetailEntity> delList= oe.getDetailList();
		List<OrderDetailEntity> orddatelist = ord.getListDetails();
		Map<Long, OrderDeliveryDetailEntity> delDetalMap = new HashMap<Long, OrderDeliveryDetailEntity>();
		Map<Long, OrderDetailEntity> orddateMap = new HashMap<Long, OrderDetailEntity>();
		
		for(int i = 0; i<delList.size(); i++){
			delDetalMap.put(delList.get(i).getSkuId(), delList.get(i));
		}
		
		for(int i = 0; i<orddatelist.size(); i++){
			orddateMap.put(orddatelist.get(i).getSkuId(), orddatelist.get(i));
		}
		BigDecimal needPay = new BigDecimal(0);
		//for(Long orderid : delDetalMap.keySet()){
		for(int i = 0;i<delList.size();i++){//  循环发货单
			Long skuid = delList.get(i).getSkuId();
			// 订货单剩余数量
			BigDecimal num = orddateMap.get(skuid).getAmount().subtract(orddateMap.get(skuid).getDeliveryAmount());
			// 发货单发货数量
			BigDecimal amount = (delDetalMap.get(skuid).getAmount()).multiply(BigDecimal.valueOf(delDetalMap.get(skuid).getUnitNum()));
			//发货后订货单的发货数量
			BigDecimal sum =  orddateMap.get(skuid).getDeliveryAmount().add(amount);
			//发货后订货单剩余的需要支付的金额, = 发货后剩余需要发货数量*单价
			needPay =  needPay.add(num.subtract(amount).multiply(orddateMap.get(skuid).getSellPrice()));
			// 订单剩余数量和发货单发货数量做对比,1大于,0等于,-1小于
			int f = num.compareTo(amount);
			if(f > 0){//发货成功,未发完
				p = -1;
			}
			if(f < 0 ){// 发货失败,返回哪条数据不符合条件
				flag = false;
				BigDecimal shorts = num.subtract(amount);
				//delDetalMap.get(skuid).setAmount(shorts);
				delList.get(i).setAmount(shorts);
			}else{// 发货成功,把剩余发货数量加上本次发货数量,用于订单上的修改
				orddateMap.get(skuid).setDeliveryAmount(sum);
				
			}
		}
		
		if(flag == true){// 发货成功,修改发货单发货状态和发货时间,同时修改订货单已发货数量和库存数量,未发货金额
			if(p ==1 ){//已发完
				ord.setOrderStatus(OrderStatus.PENDINGRECIPIENT);
			}else{//未发完
				ord.setOrderStatus(OrderStatus.PARTIALSHIP);
			}
			
			// 查看订货流程
			FactoryProcessEntity proces = factoryProcessService.selectByFacId(oe.getFacId());
			if(proces.getReceivingConfirm() == 0) { // 是否需要确认收货
				oe.setDeliveryStatus(OrderStatus.DELIVERY_SENDALL);// 需要
				if(p ==1 ){//已发完
					ord.setOrderStatus(OrderStatus.PENDINGRECIPIENT);
				}else{//未发完
					ord.setOrderStatus(OrderStatus.PARTIALSHIP);
				}
			}else {
				oe.setDeliveryStatus(OrderStatus.DELIVERY_COMPLETE);//不需要
				if(p ==1 ){//已发完
					ord.setOrderStatus(OrderStatus.COMPLETE);
				}else{//未发完
					ord.setOrderStatus(OrderStatus.PARTIALSHIP);
				}
			}
			//oe.setDeliveryStatus(OrderStatus.DELIVERY_SENDALL);
			oe.setSendDate(new Date());
			//修改发货单状态和物流
			orderDeliveryMapper.updateDelOrder(oe);
			
			//修改订单未发货金额
			ord.setNeedPay(needPay);
			orderDeliveryMapper.updateOrder(ord);
			//修改订货单已发货数量
			for(int i = 0;i<orddatelist.size();i++){
				Long skuid2 = orddatelist.get(i).getSkuId();// id
				orddatelist.get(i).setDeliveryAmount(orddateMap.get(skuid2).getDeliveryAmount());// 修改已发货数量
				orderDeliveryMapper.updateOrderDetail(orddatelist.get(i));
			}
			// 修改订单详情
			//Long orderFlag = orderDeliveryMapper.updateOrderDetail(orddatelist);

			//插入日志
			deliveryLogMapper.insert(getOrderDeliveryLogEntity(oe.getId(), 
					LogContent.DELIVERY_DELIVE, LogStatus.DELIVERY_DELIVE));
		}
		return flag;
	}

	/**
	 * 通过id 查发货单详情
	 * */
	@Override
	public OrderDeliveryInfoEntity findDeliveryDatailsById(Map<String, Object> usmap) {
		List<OrderDeliveryAllDateVo> odvList = orderDeliveryMapper.findDeliveryDatailsById(usmap);
		if(odvList.size()>0){
			OrderDeliveryInfoEntity odi = toDels(odvList).get(0);
			return odi;
		}else{
			return null;
		}
	}

	/**
	 * 发货单修改
	 * */
	@Override
	@Transactional
	public Integer updateDelOrder(OrderDeliveryInfoEntity odi) {
		Long deliveryinfoFlag = orderDeliveryMapper.updateDelOrder(odi);
		
		orderDeliveryMapper.deleteByDeliveryId(odi.getId());
		if(odi.getDetailList() != null && odi.getDetailList().size() > 0){
			orderDeliveryMapper.insertDelOrderDetail(odi.getDetailList());
		}
		
		// 发货单附件新增
		orderDeliveryFileMapper.deletBydeliveryId(odi.getId());
		List<String> fileList = odi.getFileList();
		if(null != fileList && fileList.size() > 0){
			orderDeliveryFileMapper.insertFiles(fileList,odi.getId());
		}
		//插入日志
		deliveryLogMapper.insert(getOrderDeliveryLogEntity(odi.getId(), 
				LogContent.DELIVERY_UPDATE, LogStatus.RETURN_ADD));
		if(deliveryinfoFlag >0){
			return 1;
		}else{
			return 0;
		}
	}

	/**
	 * 高级查询
	 * */
	@Override
	public void findBySenior(PageUtils<OrderDeliveryInfoEntity> pageinfo) {
		Long cont = orderDeliveryMapper.findBySeniorCont(pageinfo);
		pageinfo.setTotal(cont);
		if(cont > 0){
			List<OrderDeliveryInfoEntity> odvList = orderDeliveryMapper.findBySenior(pageinfo);
			//List<OrderDeliveryInfoEntity> list = toDels(odvList);
			pageinfo.setData(odvList);
		}else{
			pageinfo.setData(new ArrayList<OrderDeliveryInfoEntity>());
		}
	}
	
	public List<OrderDeliveryInfoEntity> toDels(List<OrderDeliveryAllDateVo> odvList){
		Set<Long> idSet = new HashSet<Long>();
		List<Long> ids = new ArrayList<Long>();// 获取id集合,用于排序
		List<OrderDeliveryInfoEntity> list = new ArrayList<OrderDeliveryInfoEntity>();
		Map<Long, OrderDeliveryInfoEntity> odMap = new HashMap<Long,OrderDeliveryInfoEntity>();
		Map<Long, OrderDeliveryDetailEntity> odeMap = new HashMap<Long,OrderDeliveryDetailEntity>();
		Map<Long, ProductDeliveryFile> pdfMap = new HashMap<Long,ProductDeliveryFile>();
		
		for(int i = 0;i<odvList.size() ;i++){
		if(idSet.add(odvList.get(i).getId())){
			ids.add(odvList.get(i).getId());
		}
		OrderDeliveryInfoEntity od= new OrderDeliveryInfoEntity();
		od.setAmountTotal(odvList.get(i).getAmountTotal());
		od.setExpressFee(odvList.get(i).getExpressFee());
		od.setAgeId(odvList.get(i).getAgeId());
		od.setAgeName(odvList.get(i).getAgeName());
		od.setCreateTime(odvList.get(i).getCreateTime());
		od.setCreator(odvList.get(i).getCreator());
		od.setDeliveryDate(odvList.get(i).getDeliveryDate());
		od.setDeliveryNo(odvList.get(i).getDeliveryNo());
		od.setDeliveryStatus(odvList.get(i).getDeliveryStatus());
		od.setExpressCompany(odvList.get(i).getExpressCompany());
		od.setExpressCompanyNo(odvList.get(i).getExpressCompanyNo());
		od.setExpressNo(odvList.get(i).getExpressNo());
		od.setFacId(odvList.get(i).getFacId());
		od.setId(odvList.get(i).getId());
		od.setIsSynchronize(odvList.get(i).getIsSynchronize());
		od.setOrigin(odvList.get(i).getOrigin());
		od.setReceiveDate(odvList.get(i).getReceiveDate());
		od.setReceiveId(odvList.get(i).getReceiveId());
		od.setDeliveryInfo(odvList.get(i).getDeliveryInfo());
		od.setReceiveInfo(odvList.get(i).getReceiveInfo());
		od.setReceiveMan(odvList.get(i).getReceiveMan());
		od.setRemark(odvList.get(i).getRemark());
		od.setSendDate(odvList.get(i).getSendDate());
		od.setSourceBillId(odvList.get(i).getSourceBillId());
		od.setSourceBillNo(odvList.get(i).getSourceBillNo());
		od.setUpdater(odvList.get(i).getUpdater());
		od.setUpdateTime(odvList.get(i).getUpdateTime());
		odMap.put(odvList.get(i).getId(), od);
		
		if(odvList.get(i).getDatailId() != null){
			OrderDeliveryDetailEntity ode = new OrderDeliveryDetailEntity();
			ode.setProductImg(odvList.get(i).getProductImg());
			ode.setAmount(odvList.get(i).getAmount());
			ode.setDeliveryId(odvList.get(i).getDeliveryId());
			ode.setId(odvList.get(i).getDatailId());
			ode.setPrice(odvList.get(i).getPrice());
			ode.setProductId(odvList.get(i).getProductId());
			ode.setProductName(odvList.get(i).getProductName());
			ode.setProductNumber(odvList.get(i).getProductNumber());
			ode.setProductSpec(odvList.get(i).getProductSpec());
			ode.setProductVolume(odvList.get(i).getProductVolume());
			ode.setProductWeight(odvList.get(i).getProductWeight());
			ode.setRemark(odvList.get(i).getDatailRemark());
			ode.setSkuId(odvList.get(i).getSkuId());
			ode.setUnitId(odvList.get(i).getUnitId());
			ode.setUnitName(odvList.get(i).getUnitName());
			ode.setUnitNum(odvList.get(i).getUnitNum());
			ode.setWhId(odvList.get(i).getWhId());
			ode.setWhName(odvList.get(i).getWhName());
			ode.setAmountPrice(odvList.get(i).getDataiAmountPrice());
			ode.setDeliveryAmount(odvList.get(i).getDeliveryAmount());
			ode.setAmountTotal(odvList.get(i).getAmountSum());
			odeMap.put(odvList.get(i).getDatailId(), ode);
		}
		if(odvList.get(i).getFileId() != null){
			ProductDeliveryFile pdf = new ProductDeliveryFile();
			pdf.setId(odvList.get(i).getFileId());
			pdf.setUrl(odvList.get(i).getUrl());
			pdf.setSeq(odvList.get(i).getSeq());
			pdf.setDeliveryId(odvList.get(i).getDeliveryId());
			pdfMap.put(odvList.get(i).getFileId(),pdf);
		}
	}
		
		for(Long id : odeMap.keySet()){
			// 获取父节点并把子节点数据放进去
			Long odId = odeMap.get(id).getDeliveryId();
			List<OrderDeliveryDetailEntity> listDetails = odMap.get(odId).getDetailList() == null ? new ArrayList<OrderDeliveryDetailEntity>() : odMap.get(odId).getDetailList();
			listDetails.add(odeMap.get(id));
			odMap.get(odId).setDetailList(listDetails);
		}
		
		//filelist
		for(Long id : pdfMap.keySet()){
			// 获取父节点并把子节点数据放进去
			Long odId = pdfMap.get(id).getDeliveryId();
			List<String> filelist = odMap.get(odId).getFileList() == null ? new ArrayList<String>() : odMap.get(odId).getFileList();
			filelist.add(pdfMap.get(id).getUrl());
			odMap.get(odId).setFileList(filelist);
		}
		/*for(Long id : odMap.keySet()){
			list.add(odMap.get(id));
		}*/
		for(int i = 0;i<ids.size();i++){
			list.add(odMap.get(ids.get(i)));
		}
		return list;
	}

	@Override
	@Transactional
	public Integer deleteDelOrder(Map<String, Object> usmap) {
		Long[] ids = (Long[]) usmap.get("ids");
		Integer sum = orderDeliveryMapper.findDeliveryStatus(usmap);
		if(sum > ids.length * OrderStatus.DELIVERY_SENDALL){// 某一个发货单已经签收,此时不能删除发货单
			return -1;
		}
		//删除附件
		orderDeliveryFileMapper.deletes(usmap);
		//删除详情
		orderDeliveryMapper.deleteByDeliveryIds(usmap);
		//删除日志
		deliveryLogMapper.deletes(ids);
		//删除头
		return orderDeliveryMapper.deleteDelOrder(usmap);
	}

	@Override
	@Transactional
	public Long updateProcess(Map<String, Object> usmap) {
		Long flag = orderDeliveryMapper.updateProcess(usmap);
		Long flag2 = orderDeliveryMapper.findDeliveryInfoByOrderId(usmap);
		if(flag2 == 0){
			usmap.put("orderStatus", OrderStatus.COMPLETE);
			orderDeliveryMapper.updateOrderStatus(usmap);
		}
		
		int status = (int) usmap.get("status");
		//插入日志
		if(status == 1) {
			deliveryLogMapper.insert(getOrderDeliveryLogEntity((long) usmap.get("id"), 
					LogContent.DELIVERY_SUBMIT, LogStatus.DELIVERY_SUBMIT));
		}else if(status == 2){
			deliveryLogMapper.insert(getOrderDeliveryLogEntity((long) usmap.get("id"), 
					LogContent.DELIVERY_AUDIT, LogStatus.DELIVERY_AUDIT));
		}else if(status == 4) {
			deliveryLogMapper.insert(getOrderDeliveryLogEntity((long) usmap.get("id"), 
					LogContent.DELIVERY_DELIVERY, LogStatus.DELIVERY_DELIVERY));
		}
		return flag;
	} 
	@Override
	public List<OrderDeliveryDetailEntity> findDetailByIdAndOrderOrigin(Map<String, Object> usmap) {
		return orderDeliveryMapper.findDetailByIdAndOrderOrigin(usmap);
	}

	@Override
	public void findDeliveryByOrderId(PageUtils<OrderDeliveryInfoEntity> pageinfo) {
		Long cont = orderDeliveryMapper.findDeliveryContByOrderId(pageinfo);
		pageinfo.setTotal(cont);
		if(cont > 0){
			List<OrderDeliveryAllDateVo> odvList = orderDeliveryMapper.findDeliveryByOrderId(pageinfo);
			List<OrderDeliveryInfoEntity> odis = toDels(odvList);
			//List<OrderDeliveryInfoEntity> list = toDels(odvList);
			pageinfo.setData(odis);
		}else{
			pageinfo.setData(new ArrayList<OrderDeliveryInfoEntity>());
		}
	}

	@Override
	public void findList(PageUtils<OrderInfoEntity> pageinfo) {
		long count = orderDeliveryMapper.findCountByOrderInfo(pageinfo);
		pageinfo.setTotal(count);
		if (count > 0) {
			pageinfo.setData(orderDeliveryMapper.findByOrderInfo(pageinfo));
		} else {
			pageinfo.setData(new ArrayList<OrderInfoEntity>());
		}
	}
}
